import entry.Library;
import entry.Goods;
import entry.Location;
import org.apdplat.word.WordSegmenter;
import org.apdplat.word.segmentation.WordRefiner;
import utils.*;

import java.util.List;
import java.util.Map;

public class Train {

    private static Library library = new Library();
    private static List<Library> libraryEachCate;
    private static List<Location> locations;

    public static void main(String[] args) {
        Long time = System.currentTimeMillis();
        //解析tsv
        InputUtil inputUtil = new InputUtil("train.tsv");
        List<Goods> goods = inputUtil.getAllGoods();
        List<List<Goods>> goodsClassify = inputUtil.classify(goods);
        //分词
        ParticipleUtils utils = new ParticipleUtils();
        libraryEachCate = utils.batchParticiple(goodsClassify);
        //组装
        pack();
        //导出标签库
        OutPutUtil.print(library);
        System.out.println("词汇量："+library.size());
        //对每一个种类进行建模，求出平均位置
        DistanceUtils distanceUtils = new DistanceUtils(library);
        locations = distanceUtils.getAveLocationEachCate(libraryEachCate);
        OutPutUtil.print(locations);
        test("Lototo日式陶瓷餐具家用餐盘菜盘早餐盘沙拉盘西餐牛排盘创意盘子 ");
        System.out.println("用时" + (System.currentTimeMillis()-time) + "ms");
    }

    //将分词结果进行提取，打包。
    public static void pack(){
        for (Library libraryACate:libraryEachCate){
            Screening screening = new Screening(libraryACate);
            //提取高频词汇
            Map<String,Integer> topN = screening.getTopN(5);
            //过滤低频词汇(提高建模速度)
            screening.filterLower();
            //组装高频词到library
            for(Map.Entry<String,Integer> entry:topN.entrySet()){
                library.put(entry.getKey(),entry.getValue());
            }
        }
    }

    //临时测试方法
    public static void test(String word){
        String goods = WordRefiner.refine(WordSegmenter.seg(word)).toString();
        String[] words = goods.substring(1,goods.length()-1).split(",");
        Location location = new Location(library,words);
        DistanceUtils distanceUtils = new DistanceUtils(library);
        String target = distanceUtils.geeMinDistancePoint(location,locations);
        System.out.println(target);
    }
}




//。。。。。。。。。。。